# -*- coding: utf-8 -*-
# @Date    : 2015-06-21 15:50:35
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

from lib.tornado_routes import route
from web.handler import BaseHandler
from tornado import gen
from tornado.web import asynchronous
from lib import libtime,libpage

from web.models.admin.user.room import Room

@route(r'/admin/user/room/?')
class ListHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        cur_page = self.get_argument('page', default=1)
        offset = self.get_argument('offset', default=self.offset)
        school_id = self.get_argument('schoolId', default=self.school_id)
        start = libpage.start(cur_page, offset)
        result = yield Room().list(school_id, start, offset)

        page_info={'total':result[0], 'page':int(cur_page)}
        self.render("admin/user/room/list.html",page_info=page_info, room_list=result[1], dorm_list=result[2])


@route(r'/admin/user/room/new/?')
class NewHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        school_id = self.get_argument('schoolId', default=self.school_id)
        result = yield Room().get_dorm_list(school_id)
        self.render("admin/user/room/new.html",dorm_list=result)

    @asynchronous
    @gen.coroutine
    def post(self):
        dorm_id = self.get_argument('dormId', default=0)
        room_name = self.get_argument('roomName', default='')
        school_id = self.get_argument('schoolId', default=self.school_id)
        remark = self.get_argument('remark', default='')
        result = yield Room().new(room_name, dorm_id, school_id, remark)
        self.redirect('/admin/user/room')

@route(r'/admin/user/room/edit/?')
class EditHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        room_id = self.get_argument('roomId',default=0)
        school_id = self.get_argument('schoolId', default=self.school_id)
        dorm_list = []
        room_info = {}
        if room_id == 0:
            self.write_error(404)
        else:
            result = yield Room().edit_info(school_id, room_id)
            if len(result) > 0:
                dorm_list = result[1]
                room_info = result[0]
            else:
                self.write_error(404)
        self.render("admin/user/room/edit.html", room_info=room_info,dorm_list=dorm_list)

    @asynchronous
    @gen.coroutine
    def post(self):
        room_id = self.get_argument('roomId',default=0)
        room_name = self.get_argument('roomName', default='')
        school_id = self.get_argument('schoolId', default=self.school_id)
        remark = self.get_argument('remark', default='')
        dorm_id = self.get_argument('dormId', default='')
        result = yield Room().edit(room_id, room_name, dorm_id, school_id, remark)
        if result == 0:
            print '更新失败'
        elif result > 0:
            print '更新成功'
        self.redirect('/admin/user/room')


@route(r'/admin/user/room/del/?')
class DelHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        room_id = self.get_argument('roomId',default=0)
        school_id = self.get_argument('schoolId', default=self.school_id)
        info_list = []
        if room_id == 0:
            self.write_error(404)
        else:
            result = yield Room().delete(room_id,school_id)
        self.redirect('/admin/user/room')


@route(r'/admin/user/room/search/?')
class SearchHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        room_name = self.get_argument('roomName',default='')
        dorm_id = self.get_argument('dormId',default='')
        school_id = self.get_argument('schoolId', default=self.school_id)
        cur_page = self.get_argument('page', default=1)
        offset = self.get_argument('offset', default=self.offset)
        start = libpage.start(cur_page, offset)
        result = yield Room().search(room_name,dorm_id,school_id,start,offset)

        if len(result) > 0:
            page_info = {'total':result[0], 'page':int(cur_page)}
        else:
            page_info = {'total':0, 'page':0}
        ser_list = [room_name, dorm_id]
        self.render("admin/user/room/list.html",page_info=page_info, room_list=result[1], dorm_list=result[2], ser_list=ser_list)



